from django.contrib import auth
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext

def login(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            # Correct password, and the user is marked "active"
            auth.login(request, user)
            # Redirect to a success page.
            return HttpResponseRedirect("/friend/dashboard/")
        else:
            # Show an error page
            return HttpResponseRedirect("/accounts/login/")

    return render_to_response(
                "accounts/login.html"
                , {
                }
                , context_instance=RequestContext(request))

